package game.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;


/*JDBC 
 * 相关工具类
 * 
 * */
public class ss
{
	/*访问数据库
	 * CRUD 都要先登录数据库
	 * 所以把登录数据库的代码
	 * 写成全局变量
	 * */
	private static String driver = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql:///game?useSSL=false";
	private String user = "root";
	private String password = "123456";
	/*加载驱动 程序执行的时候
	 * 始终只执行一次并且 先执行  
	 * 用静态代码块
	 * */
	static
	{
		try
		{
			Class.forName(driver);
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	/*获取数据库连接
	 * connection
	 * 可以封装成一个方法 
	 * */
	public Connection getConnection()
	{
		Connection connection;
		try
		{
			connection = DriverManager.getConnection( url, user, password);
			return connection;
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		/*程序执行到此处返回值为空*/
		return null;
	}
	/*在连接数据库中
	 * 关闭连接也是重复代码
	 * 需要封装方法
	 * */
	public void closeAll(Connection connection ,Statement st , ResultSet rs )
	{
		try
		{
			if(rs !=null)
			{
				rs.close();
				rs = null;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		try
		{
			if(st !=null)
			{
				st.close();
				st = null;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		try
		{
			if(st != null)
			{
				connection.close();
				connection = null ;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
	public static void mian(String[] args)
	{
		System.out.println("==");
		/*查询一张表*/
		String sql = "select * from a_demo";
		DBUtil db = new DBUtil();
		/*获取连接*/
		Connection connection =db.getConnection();
		/*预处理--占位符*/
		PreparedStatement pst = null ;
		ResultSet res = null ;
		try
		{
			pst = connection.prepareStatement(sql);
			res = pst.executeQuery();
			System.out.println("id\tname\tcontent\tstatus\tcreatrTime\tupdateTime\tpubTime");
			while(res.next())
			{
				String id = res.getString("id");
				String name = res.getString("name");
				String content = res.getString("content");
				byte status = res.getByte("status");
				Date creatrTime = res.getTimestamp("creatrTime");
				Date updateTime = res.getTimestamp("updeteTime");
				Date pubTime = res.getTimestamp("pubTime");
				System.out.println(id+"\t"+name+"\t"+content+"\t"
						+status+"\t"+creatrTime.toLocaleString()+"\t"
						+updateTime.toLocaleString()+"\t"+pubTime.toLocaleString());
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}finally
		{
			/*每次都要进行此操作
			 * 直接封个方法调用就好了
			 * */
			/*Statement和RreparedStatement
			 * 是父子接口  可以直接调用
			 * */
			db.closeAll(connection, pst, res);
		}
	}
	private static void standardJDBC()
	{
		//查询一张表
		String sql = "select * from a_demo";
		DBUtil db = new DBUtil();
		//获取连接
		Connection connection =db.getConnection();
		//预处理--占位符
		Statement st = null ;
		ResultSet rs = null ;
		try
		{
			st = connection.createStatement();
			rs = st.executeQuery(sql);
			System.out.println("id\tname\tcontent\tstatus\tcreatrTime\tupdateTime\tpubTime");
			while(rs.next())
			{
				String id = rs.getString("id");
				String name = rs.getString("name");
				String content = rs.getString("content");
				byte status = rs.getByte("status");
				Date creatrTime = rs.getTimestamp("creatrTime");
				Date updateTime = rs.getTimestamp("updeteTime");
				Date pubTime = rs.getTimestamp("pubTime");
				System.out.println(id+"\t"+name+"\t"+content+"\t"
						+status+"\t"+creatrTime.toLocaleString()+"\t"
						+updateTime.toLocaleString()+"\t"+pubTime.toLocaleString());
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				if(rs !=null)
				{
					rs.close();
					rs = null;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(st !=null)
				{
					st.close();
					st = null;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(st != null)
				{
					connection.close();
					connection = null ;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}
